package com.lxd.sunso.product.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;


import com.lxd.sunso.common.utils.DateUnit;
import com.lxd.sunso.common.utils.PageUtils;
import com.lxd.sunso.common.utils.R;
import com.lxd.sunso.product.vo.LaAndLoVo;
import com.lxd.sunso.product.vo.MachineNameAndIdVo;
import com.lxd.sunso.product.vo.MessageVo;
import org.apache.xmlbeans.impl.xb.xsdschema.Public;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.lxd.sunso.product.entity.MachineEntity;
import com.lxd.sunso.product.service.MachineService;
import org.springframework.web.multipart.MultipartFile;


/**
 * @author lxd
 * @email sunlightcs@gmail.com
 * @date 2020-12-30 18:06:30
 */
@RestController
@RequestMapping("product/machine")
public class MachineController {
    @Autowired
    private MachineService machineService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:machine:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = machineService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("product:machine:info")
    public R info(@PathVariable("id") Integer id) {
        MachineEntity machine = machineService.findById(id);
        return R.ok().put("machine", machine);
    }

    /**
     * 保存设备以及设备初始数据置0
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:machine:save")
    public R save(@RequestBody MachineEntity machine) {
        machine.setCreatetime(DateUnit.dateFormat(new Date()));
        int i = machineService.saveAndset(machine);
        if (i == 0) {
            return R.ok();
        } else if (i == 1) {
            return R.error(1, "设备ID已使用");
        } else {
            return R.error(1, "CRC8校验码错误");
        }
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:machine:update")
    public R update(MultipartFile file, MachineEntity machine) {
        machineService.newUpdate(machine, file);
        return R.ok();
    }


    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:machine:delete")
    public R delete(@RequestBody Long[] ids) {
        machineService.delete(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 根据条件搜索
     */
    @RequestMapping("/search")
    public R search(@RequestParam Map<String, Object> params) {
        PageUtils page = machineService.search(params);
        return R.ok().put("page", page);
    }

    /**
     * 获取与该设备同模板同顾客的设备
     *
     * @param params
     * @return
     */
    @RequestMapping("/searchBatchMachine")
    public R searcBatchMachine(@RequestParam Map<String, Object> params) {
        List<MachineEntity> machineEntities = machineService.searchBatchMchine(params);
        return R.ok().put("list", machineEntities);
    }

    /**
     * 获取客户的设备总数
     */
    @RequestMapping("/getCusMachine")
    public R getCusMachine() {
        List<MessageVo> list = machineService.getCusMachine();
        return R.ok().put("data", list);
    }

    /**
     * 客户在线设备统计
     */
    @RequestMapping("/getCusMachineOn")
    public R getCusMachineOn() {
        List<MessageVo> list = machineService.getCusMachineOn();
        return R.ok().put("data", list);
    }

    /**
     * 根据型号，在线设备
     */
    @RequestMapping("/getKindMachine")
    public R getKindMachine() {
        List<MessageVo> list = machineService.getKindMachine();
        return R.ok().put("data", list);
    }

    /**
     * 根据型号，所有设备
     */
    @RequestMapping("/getKindMachineAll")
    public R getKindMachineAll() {
        List<MessageVo> list = machineService.getKindMachineAll();
        return R.ok().put("data", list);
    }

    /**
     * 获取所有设备的名称以及ID
     */
    @RequestMapping("/getMachineNameAndId")
    public R getMachineNameAndId(@RequestParam Map<String, Object> params) {
        List<Object> list = machineService.getMachineNameAndId(params);
        return R.ok().put("page", list);
    }

    /**
     * 获取设备的经纬度
     */
    @RequestMapping("/getLaAndLo")
    public R getLaAndLo(@RequestParam Map<String, Object> params) {
        List<LaAndLoVo> list = machineService.getLaAndLo(params);
        return R.ok().put("page", list);
    }

    /**
     * 获取数据中心文字显示
     */
    @RequestMapping("/data")
    public R getData() {
        Map<String, Integer> data = machineService.getData();
        return R.ok().put("data", data);
    }

    /**
     * 获取所有在线的设备
     */
    @RequestMapping("/getOnlineMachie")
    public R getOnlineMahcine() {
        List<MachineNameAndIdVo> list = machineService.getOnlineMahcine();
        return R.ok().put("list", list);
    }
}
